آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

یک مشکل عجیب تو مدیا پلیر

novinfekr  11 سال پیش  11 سال پیش
+1 0

من این کد رو نوشتم و گفتم با کلیک بر روی btnProgressBar در صورتی که فایل صوتی وجود داشت پخش شود و در غیر اینصورت فایل را دانود کرده و پخش شود.اما مشکلی که من دارم.من دو تا ایمج ویو هم برای pause و play کردن گزاشتم ولی در صورتی که فایل وجود داشته باشه با ایمیج ویو play فایل شروع به پخش نمیکند.؟لطفا راهنمایی کنید کجای کد مشکل داره یا چی باید اضافه بشه؟

 package ir.mrmoradi.samadie;

import java.io.File;
import java.io.IOException;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;


public class Dars1Bakhsh1 extends MainActivity {

    // AQuery object
    AQuery             aq;
    // Progress Dialog Bar
    ProgressDialog     prgDialog;
    // Mp3 File location
    String             url = "http://mrmoradi.ir/001_1.mp3";
    // Media player object
    public MediaPlayer mediaplayer;

    SeekBar            seekBar;

    ImageView          pause_btn;
    ImageView          play_btn;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dars1bakhsh1);

        // Instantiate AQuery object
        aq = new AQuery(this);
        // Look for button click event using AQuery clicked() method
        aq.id(R.id.btnProgressBar).clicked(this, "downloadSongandPlay");

        pause_btn = (ImageView) findViewById(R.id.pause_btn);
        play_btn = (ImageView) findViewById(R.id.play_btn);

        seekBar = (SeekBar) findViewById(R.id.slider);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}


            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}


            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {

                if (fromUser) {
                    // vaghti seek bar ro jabeja mikonim inja ejra mishe...
                    mediaplayer.seekTo(progress);

                }
            }
        });

        // Font path
        String fontPath = "fonts/BYekan.ttf";

        // text view label
        TextView txttitle_dars1bakhsh1 = (TextView) findViewById(R.id.txttitle_dars1bakhsh1);
        TextView txttozih = (TextView) findViewById(R.id.txttozih);
        TextView txtdownloadtoz = (TextView) findViewById(R.id.txtdownloadtoz);

        // Loading Font Face
        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

        // Applying font
        txttitle_dars1bakhsh1.setTypeface(tf);
        txttozih.setTypeface(tf);
        txtdownloadtoz.setTypeface(tf);

    }


    // AQuery button click callback method
    public void downloadSongandPlay(View v) {
        // Mp3 file location in SD card
        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/samadie/sdasdsd");
        // Get SD card location
        File ext = Environment.getExternalStorageDirectory();
        // Target location where downloaded file to be stored
        File target = new File(ext, "samadie/sdasdsd");

        // Disable button in order to avoid multiple button hits
        aq.id(R.id.btnProgressBar).enabled(false);
        // When Mp3 File exists under SD card
        if (file.exists()) {
            Toast.makeText(getApplicationContext(), "در حال پخش", Toast.LENGTH_LONG).show();
            // Play Music
            playMusic();

            // If the Music File doesn't exist in SD card (Not yet downloaded)
        } else {
            Toast.makeText(getApplicationContext(), "صبور باش برادر ، سرعت دانلود به سرعت اینترنت شما بستگی داره...", Toast.LENGTH_LONG).show();
            prgDialog = new ProgressDialog(this); // Instantiate Progress Dialog Bar
            prgDialog.setMessage("فایل صوتی یافت نشد ، در حال دانلود ..."); // Set Progress Dialog Bar message
            prgDialog.setIcon(R.drawable.download);
            prgDialog.setTitle("در حال دانلود");
            prgDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            prgDialog.setIndeterminate(false);
            prgDialog.setMax(100); // Progress Bar max limit
            prgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // Progress Bar style
            prgDialog.setCancelable(false); // Progress Bar cannot be cancelable
            // Display progress dialog bar and initiate download of Mp3 file
            aq.progress(prgDialog).download(url, target, new AjaxCallback<File>() {

                // Once download is complete
                @Override
                public void callback(String url, File file, AjaxStatus status) {
                    // If file does exist
                    if (file != null) {
                        playMusic();

                        // If file doesn't exist display error message
                    } else {
                        Toast.makeText(aq.getContext(), "یک خطا رخ داد در وضعیت : " + status,
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }


    protected void playMusic() {
        // Read Mp3 file present under SD card
        Uri myUri1 = Uri.parse("file:///sdcard/samadie/sdasdsd");
        mediaplayer = new MediaPlayer();
        mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaplayer.setDataSource(aq.getContext(), myUri1);
            mediaplayer.prepare();
            // Start playing the Music file
            mediaplayer.start();
            mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    seekBar.setMax(mediaplayer.getDuration());
                    seekBar.postDelayed(onEverySecond, 1000);
                    pause_btn.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View view) {
                            mediaplayer.pause();
                        }
                    });

                    play_btn.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View view) {

                            mediaplayer.start();

                            // tanzim e dobareye seekbar bad az inke dobare play kardim
                            seekBar.setMax(mediaplayer.getDuration());
                            seekBar.postDelayed(onEverySecond, 1000);

                        }
                    });

                }
            });

            mediaplayer.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    // Once Music is completed playing, enable the button
                    aq.id(R.id.btnProgressBar).enabled(true);
                    Toast.makeText(getApplicationContext(), "با موفقیت درس این بخش را دانبود کردید ، درس هم اکنون در حال پخش...", Toast.LENGTH_LONG).show();
                }
            });
        }
        catch (IllegalArgumentException e) {
            Toast.makeText(getApplicationContext(), "یک مشکلی واسه دانلود پیش اومده ولی غصه نخوری ها ، به برنامه نویس ( آقای مرادی ) ایمیل بده تا سریع مشکل رو برطرف کنه. !", Toast.LENGTH_LONG).show();
            //You might not set the URI correctly!
        }
        catch (SecurityException e) {
            Toast.makeText(getApplicationContext(), "مشکل دانلود در اتصال به سایت ، به برنامه نویس ( آقای مرادی ) اطلاع بدید. با تشکر", Toast.LENGTH_LONG).show();
            //URI cannot be accessed, permissed needed
        }
        catch (IllegalStateException e) {
            Toast.makeText(getApplicationContext(), "مدیا پلیر گوشیت مشکل داره ، یک مدیا پلیر دیگه نصب کن دوباره امتحان کن", Toast.LENGTH_LONG).show();
            //Media Player is not in correct state
        }
        catch (IOException e) {
            Toast.makeText(getApplicationContext(), "فرمت فایل صوتی این درس با ورژن اندروید شما نمیخونه برادر ، به برنامه نویس ( آقای مرادی ) خبر بده. با تشکر", Toast.LENGTH_LONG).show();
            //IO Error occured
        }
    }

    private Runnable onEverySecond = new Runnable() {

                                       @Override
                                       public void run() {
                                           if (seekBar != null) {
                                               seekBar.setProgress(mediaplayer.getCurrentPosition());
                                           }

                                           if (mediaplayer.isPlaying()) {
                                               seekBar.postDelayed(onEverySecond, 1000);
                                           }
                                       }
                                   };


    @Override
    public void onBackPressed() {
        mediaplayer.pause();
        Intent intent = new Intent(G.CurrentActivity, Dars1.class);
        G.CurrentActivity.startActivity(intent);
        G.CurrentActivity.finish();

    }
}
0 0
شما playMusic() رو در onCreate فراخوانی نکردید. (11 سال پیش)
0 0
منم چند بار که داشتم با media player کار میکردم با مشکلات عجیب غریب برخورد کردم که واقعا خودمم موندم دلیلش چی بود ولی یک بار اسم فایل اهنگ رو عوض کرده بودم که درست شد مشکل یک بار دیگه هم متغیر media player که به صورت سراسری تعریف شده بود ، محلی تعریف کردم مشکل حل شد (11 سال پیش)
 برای این سوال پاسخی وجود ندارد.

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .