انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
مشکل در پخش دو فایل صوتی،تصویری از هاست (برنامه نویسی اندروید)

مشکل در پخش دو فایل صوتی،تصویری از هاست (برنامه نویسی اندروید)

#1
سلام ی  پروژه پخش فایل های صوتی تصویری  بصورت انلاین و دانلود دارم  به این صورت ک دو فایل رو از روی یک هاست از دو پوشه میخونه منتها فقط پروژه قادر ه ک فقط از یک از ادرس ها فایل ها رو بخونه.
یعنی وقتی روی دکمه پخش فایل تصویری میزنم لیست فایل ها رو بهم نشون میده ولی وقتی رو دکمه صوتی میزنم قبل از اینکه لیست فایل بهم نشون بده پروژه کرش میکنه ومیگه پروژه متوقف شده است.



سورس پروژه به این صورته ک یه فایل Main برای پخش فایل صوتی  



کد:
public class Main_sound_rs extends ListActivity {
    private ProgressDialog pd;
    private String res="";
    private String [] name;
    private ListView list1;
    
    
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main_souti_rs);
       
       list1=(ListView) findViewById(R.id.list1);
       
      File f=new File (Environment.getExternalStorageDirectory().toString()+"/myfile");
   
       if (!f.isDirectory()){
           f.mkdir();
       }
       pd=ProgressDialog.show(Main_sound_rs.this, "", "Please wait ...");
       new getlist().execute();
   }
   
   @Override
   protected void onListItemClick(ListView list1, View v, int position, long id) {
 
       super.onListItemClick(list1, v, position, id);
       
       Intent i=new Intent(Main_sound_rs.this,Media_sound_rs.class);
       i.putExtra("name",name[position]);
       startActivity(i);
   }
   
   
   
   private class getlist extends AsyncTask{

        @Override
        protected Object doInBackground(Object... arg0) {
        
            try{
                
                URL url= new URL("http://xxxxxxxxxxxxxxxxxx.ir/sounds/rs/getlistfile.php");
                URLConnection con =url.openConnection();
                BufferedReader reader= new BufferedReader(new InputStreamReader(con.getInputStream()));
                StringBuilder sb=new StringBuilder();
                String line=null;
                
                while((line=reader.readLine())!=null){
                    
                    sb.append(line);
                    
                }
                
                res=sb.toString();
                
            }catch(Exception e){
                res=e.toString();
                
            }
        
            return "";
        }
       
        
        @Override
        protected void onPostExecute(Object result) {
            
            super.onPostExecute(result);
            name=res.split("♥");
            setListAdapter(new AA());
            pd.dismiss();
        }
     }
   
    private class AA extends ArrayAdapter<String>{

        public AA( ) {
            super(Main_sound_rs.this,R.layout.row_listfile_sound,name);
            
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            
            LayoutInflater in=getLayoutInflater();
            View row=in.inflate(R.layout.row_listfile_sound, parent,false);
            
            TextView Name=(TextView) row.findViewById(R.id.row_listname);
            
            
            Name.setText(name[position].subSequence(0,name[position].length()-1));
            
            
            
            return row;
        }
       
       
    }
   }


یه فایل Main  برای پخش فایل تصویری.

کد:
public class Main_video_barayan extends ListActivity {
    private ProgressDialog pd;
    private String res="";
    private String [] name;
    
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main_barayan);
      File f=new File (Environment.getExternalStorageDirectory().toString()+"/myfile");
   
       if (!f.isDirectory()){
           f.mkdir();
       }
       pd=ProgressDialog.show(Main_video_barayan.this, "", "Please wait ...");
       new getlist().execute();
   }
   
   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
 
       super.onListItemClick(l, v, position, id);
       
       Intent i=new Intent(Main_video_barayan.this,Media_video_brayan.class);
       i.putExtra("name",name[position]);
       startActivity(i);
   }
   
   
   
   private class getlist extends AsyncTask{

        @Override
        protected Object doInBackground(Object... arg0) {
        
            try{
                
                URL url= new URL("http://xxxxxxxxxxxxxx.ir/video/brayan/getlistfile.php");
                URLConnection con =url.openConnection();
                BufferedReader reader= new BufferedReader(new InputStreamReader(con.getInputStream()));
                StringBuilder sb=new StringBuilder();
                String line=null;
                
                while((line=reader.readLine())!=null){
                    
                    sb.append(line);
                    
                }
                
                res=sb.toString();
                
            }catch(Exception e){
                res=e.toString();
                
            }
        
            return "";
        }
       
        
        @Override
        protected void onPostExecute(Object result) {
            
            super.onPostExecute(result);
            name=res.split("♥");
            setListAdapter(new AA());
            pd.dismiss();
        }
     }
   
    private class AA extends ArrayAdapter<String>{

        public AA( ) {
            super(Main_video_barayan.this,R.layout.row_listfile,name);
            
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            
            LayoutInflater in=getLayoutInflater();
            View row=in.inflate(R.layout.row_listfile, parent,false);
            
            TextView Name=(TextView) row.findViewById(R.id.row_name);
            
            
            Name.setText(name[position].subSequence(0,name[position].length()-1));
            
            
            
            return row;
        }
       
       
    }
   }

واما کلاس مربوط به فایل صوتی برای پخش فایل و گرفتن ادرس از هاست.
کد:
public class Media_sound_rs extends Activity{
    private  String fname="";
    private int sw=0;
    private MediaController mc;
    private ProgressDialog pd;
    private VideoView v;

    int dlsize=0,cdl=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.sound);
        
        v=(VideoView) findViewById(R.id.sounds);
        
        Bundle ex=getIntent().getExtras();
        fname=ex.getString("name");
        
        File f=new File(Environment.getExternalStorageDirectory().toString()+"/myfile");
        File fs[] = f.listFiles();
        
        for(int i=0; i<fs.length;i++){
            if(fs[i].getName().toString().equals(fname)){
                sw=1;
            }
            
        }
        
        if(sw==0){
            
            dialog();
            
        }else{
            
            showvideofromsd();
        }
    }
    private void showvideofromsd(){
        v.setVideoPath(Environment.getExternalStorageDirectory().toString()+"/myfile/"+fname);
        mc=new MediaController(Media_sound_rs.this);
        v.setMediaController(mc);
        v.start();
    }
    
    
    private void dialog(){
        
        
        final Dialog di=new Dialog(Media_sound_rs.this);
        di.setContentView(R.layout.dialog);
        di.setTitle("فایل"+fname);
        
        TextView online=(TextView)di.findViewById(R.id.dialog_online);
        TextView dl=(TextView)di.findViewById(R.id.dialog_dl);
        
        online.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                
                showonline();
                di.dismiss();
                
            }
        });
        dl.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                di.dismiss();
                pd=ProgressDialog.show(Media_sound_rs.this,"","Downloading... 0%");
                new Thread(new Runnable(){
                @Override
                public void run(){
                    fdl();
                }
                
                
            }).start();
            }
        });

        di.show();
    }



private void showonline(){
    
    try{
        
        pd=ProgressDialog.show(Media_sound_rs.this, "", "Buffering...",false);
         String Link="http://xxxxxxxxxxxxxx.ir/sounds/RS/files/"+fname.replace(" ", "%20");

         Uri l=Uri.parse(Link);
        mc=new MediaController(Media_sound_rs.this);
        v.setMediaController(mc);
        v.setVideoURI(l);
        
        v.setOnPreparedListener(new OnPreparedListener() {
            
            @Override
            public void onPrepared(MediaPlayer arg0) {
            
                pd.dismiss();
                v.start();
                
            }
        });

    }catch(Exception e){
        
    }
    
}
    

private void fdl(){
    
    
    try{
        String link="http://xxxxxxxxxxxxxxxxx.ir/sounds/RS/files/"+fname.replace(" ", "%20");
        
        URL url=new URL(link);
        HttpURLConnection urlc=(HttpURLConnection) url.openConnection();
        
        urlc.setRequestMethod("GET");
        urlc.setDoOutput(true);
        urlc.connect();
        dlsize=urlc.getContentLength();
        File f=new File(Environment.getExternalStorageDirectory().toString()+"/myfile/"+fname);

        FileOutputStream fo=new FileOutputStream(f);
        
        InputStream ins=urlc.getInputStream();
        
        
        byte[] buffer=new byte[1024];
        
        int bl=0;
        while((bl=ins.read(buffer))>0){
            
            fo.write(buffer, 0, bl);
            cdl+=bl;
            
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    
                    float o=((float)cdl/dlsize)*100;
                    
                    pd.setMessage("Downloading ... "+o+"%");
                    
                    
                }
            });
    
        }
        
        fo.close();
        
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                
                pd.dismiss();
                showvideofromsd();
                
            }
        });
    
    }catch(final Exception e){
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                
                pd.dismiss();
                Toast.makeText(getApplicationContext(), e.toString(), 3000).show();
                File f=new File(Environment.getExternalStorageDirectory().toString()+"/myfile/"+fname);
                f.delete();
                finish();
            }
        });
    
    }
    
 }

}


واما کلاس مربوط به فایل تصویری  برای پخش فایل و گرفتن ادرس از هاست.
کد:
public class Media_video_brayan extends Activity{
    private  String fname="";
    private int sw=0;
    private MediaController mc;
    private ProgressDialog pd;
    private VideoView v;
    
    int dlsize=0,cdl=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.svideo);
        
        v=(VideoView) findViewById(R.id.svideo_vide);
        
        Bundle ex=getIntent().getExtras();
        fname=ex.getString("name");
        
        File f=new File(Environment.getExternalStorageDirectory().toString()+"/myfile");
        File fs[] = f.listFiles();
        
        for(int i=0; i<fs.length;i++){
            if(fs[i].getName().toString().equals(fname)){
                sw=1;
            }
            
        }
        
        if(sw==0){
            
            dialog();
            
        }else{
            
            showvideofromsd();
        }
    }
    private void showvideofromsd(){
        v.setVideoPath(Environment.getExternalStorageDirectory().toString()+"/myfile/"+fname);
        mc=new MediaController(Media_video_brayan.this);
        v.setMediaController(mc);
        v.start();
    }

    private void dialog(){

        final Dialog di=new Dialog(Media_video_brayan.this);
        di.setContentView(R.layout.dialog);
        di.setTitle("فایل"+fname);
        
        TextView online=(TextView)di.findViewById(R.id.dialog_online);
        TextView dl=(TextView)di.findViewById(R.id.dialog_dl);
        
        online.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                
                showonline();
                di.dismiss();
                
            }
        });
        dl.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                di.dismiss();
                pd=ProgressDialog.show(Media_video_brayan.this,"","Downloading... 0%");
                new Thread(new Runnable(){
                @Override
                public void run(){
                    fdl();
                }
                
                
            }).start();
            }
        });

        di.show();
    }

private void showonline(){
    
    try{
        
        pd=ProgressDialog.show(Media_video_brayan.this, "", "Buffering...",false);
         String Link="http://xxxxxxxxxxxxxxxx.ir/video/brayan/files/"+fname.replace(" ", "%20");

         Uri l=Uri.parse(Link);
        mc=new MediaController(Media_video_brayan.this);
        v.setMediaController(mc);
        v.setVideoURI(l);
        
        v.setOnPreparedListener(new OnPreparedListener() {
            
            @Override
            public void onPrepared(MediaPlayer arg0) {
            
                pd.dismiss();
                v.start();
                
            }
        });

    }catch(Exception e){
        
    }
    
}
    

private void fdl(){
    
    
    try{
        String link="http://xxxxxxxxxxxxxxxx.ir/video/brayan/files/"+fname.replace(" ", "%20");
        
        URL url=new URL(link);
        HttpURLConnection urlc=(HttpURLConnection) url.openConnection();
        
        urlc.setRequestMethod("GET");
        urlc.setDoOutput(true);
        urlc.connect();
        dlsize=urlc.getContentLength();
        File f=new File(Environment.getExternalStorageDirectory().toString()+"/myfile/"+fname);

        FileOutputStream fo=new FileOutputStream(f);
        
        InputStream ins=urlc.getInputStream();

        byte[] buffer=new byte[1024];
        
        int bl=0;
        while((bl=ins.read(buffer))>0){
            
            fo.write(buffer, 0, bl);
            cdl+=bl;
            
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    
                    float o=((float)cdl/dlsize)*100;
                    
                    pd.setMessage("Downloading ... "+o+"%");

                }
            });
    
        }
        
        fo.close();
        
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                
                pd.dismiss();
                showvideofromsd();
                
            }
        });
    
    }catch(final Exception e){
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                
                pd.dismiss();
                Toast.makeText(getApplicationContext(), e.toString(), 3000).show();
                File f=new File(Environment.getExternalStorageDirectory().toString()+"/myfile/"+fname);
                f.delete();
                finish();
            }
        });
    
    }
    
 }

}


لازم ب ذکر که فایل getlistfile.php هم ک توی هر دو ادرس فقط کپی پیست کردم و هیچ تغییری ندادم یعنی همون طور ک تو پوشه ویدیو روی هاست لیست فایل ها رو نشون میده تو پوشه صدا هم فقط کپی پیست کردم.
ممنون میشم دوستان راهنمایی کنند.
پاسخ


پیام‌های این موضوع
مشکل در پخش دو فایل صوتی،تصویری از هاست (برنامه نویسی اندروید) - توسط esikley - ۱۳۹۴/۱۰/۲۴, ۱۲:۵۵ ب.ظ

پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان