简体   繁体   English

MediaPlayer可在模拟器上运行,但不能在android中的实际设备上运行

[英]MediaPlayer working on emulator, but not on actual device in android

I am creating an application where music is streaming from direct link. 我正在创建一个应用程序,其中音乐从直接链接流式传输。 It is working with emulator but not working on actual device. 它可以在模拟器上运行,但不能在实际设备上运行。 I created a class named HomeFragment and a nested class Player which extends AsyncTask and initailized MediaPlayer. 我创建了一个名为HomeFragment的类和一个嵌套的Player类,该类扩展了AsyncTask和initailized MediaPlayer。 It looks like that there is error on intantiating MediaPlayer. 似乎在启动MediaPlayer时出错。 In the logcat it shows prepare() failed. 在logcat中,它显示prepare()失败。

I have searched for it and found Attempt to call getDuration without a valid mediaplayer in media player on android ,but this is defferent because i am not getting error because of getDuration() method 我已经搜索了它,发现尝试在Android上的媒体播放器中尝试在没有有效媒体播放器的情况下调用getDuration ,但这是不同的,因为我没有因为getDuration()方法而出错

    public class HomeFragment extends Fragment {
View view;

private ImageButton btn_play_pause;
private TextView tv_time;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int mediaFileLength;
private int realTimeLength;
private boolean playPause;
private boolean initialStage = true;
private ProgressDialog progressDialog;
final Handler handler = new Handler();
private Runnable updater;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.home_fragment,container,false);
    initialize();
    return view;
}

private void initialize() {
    btn_play_pause = (ImageButton) view.findViewById(R.id.btn_play_pause);
    tv_time = (TextView) view.findViewById(R.id.tv_timer);
    seekBar = (SeekBar) view.findViewById(R.id.seekBar);
    seekBar.setMax(99);
    seekBar.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(mediaPlayer.isPlaying()){
                SeekBar seekbar = (SeekBar) v;
                int playPosition = (mediaFileLength / 100) * seekbar.getProgress();
                mediaPlayer.seekTo(playPosition);
            }
            return false;
        }
    });

    mediaPlayer = new MediaPlayer();
    progressDialog = new ProgressDialog(view.getContext());
    btn_play_pause.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if(!playPause){
                btn_play_pause.setImageResource(R.drawable.ic_pause);
                if(initialStage){
                    new Player().execute("http://www.url_of_song.mp3");
                }
                else {
                    if (mediaPlayer != null) {
                        if (!mediaPlayer.isPlaying()) {
                            mediaPlayer.start();
                        }
                    }
                }
                playPause = true;
            }
            else{
                btn_play_pause.setImageResource(R.drawable.ic_play);
                if(mediaPlayer != null) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                    }
                }
                playPause = false;
            }
        }
    });
}

class Player extends AsyncTask<String, Void, Boolean>{

    @Override
    protected Boolean doInBackground(String... params) {
        boolean prepared = false;

        try{
            mediaPlayer.setDataSource(params[0]);
            mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                @Override
                public void onBufferingUpdate(MediaPlayer mp, int percent) {
                    seekBar.setSecondaryProgress(percent);
                }
            });

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){

                @Override
                public void onCompletion(MediaPlayer mp) {
                    initialStage = true;
                    playPause = false;
                    btn_play_pause.setImageResource(R.drawable.ic_pause);
                    mp.stop();
                    mp.reset();
                }
            });
            mediaPlayer.prepare();
            prepared = true;
        }
        catch (Exception e){
            Log.e("AudioStreaming", e.getMessage());
            prepared = false;
        }
        return prepared;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog.setMessage("Please Wait...");
        progressDialog.show();
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        if(progressDialog.isShowing()){
            progressDialog.cancel();
        }
   //     mediaFileLength = mediaPlayer.getDuration();
        realTimeLength = mediaFileLength;
        mediaPlayer.start();
        initialStage = false;
        updateSeekbar();
    }
}

private void updateSeekbar(){
    seekBar.setProgress((int)(((float)mediaPlayer.getCurrentPosition() / mediaFileLength)*100));
    if(mediaPlayer.isPlaying()){
        updater = new Runnable() {
            @Override
            public void run() {
                updateSeekbar();
                realTimeLength -= 1000;
                tv_time.setText(String.format("%d:%d", TimeUnit.MILLISECONDS.toMinutes(realTimeLength), TimeUnit.MILLISECONDS.toSeconds(realTimeLength) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(realTimeLength))));
            }
        };
        handler.postDelayed(updater, 1000);
    }
}

What i am doing wrong, what is the error, The Logcat shows: 我在做什么错了,这是什么错误,Logcat显示:

    02-03 15:54:27.226 15845-15845/com.example.sk.voiceapplication I/ViewRootImpl: CPU Rendering VSync enable = true
02-03 15:54:27.266 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:27.266 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:29.496 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:33.126 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:33.126 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:36.926 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:36.926 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:40.486 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:40.486 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:44.326 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:44.336 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:48.176 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:48.176 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:51.696 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:51.696 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:55.526 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:55.526 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:54:59.366 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:54:59.366 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:03.206 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:55:03.206 15845-15863/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:07.046 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1)
02-03 15:55:07.046 15845-15864/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:07.886 15845-15991/com.example.sk.voiceapplication D/MediaHTTPConnection: proxy null port 0
02-03 15:55:08.536 15845-15991/com.example.sk.voiceapplication E/MediaPlayer: error (1, -2147483648)
02-03 15:55:08.536 15845-15878/com.example.sk.voiceapplication E/AudioStreaming: Prepare failed.: status=0x1

02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: error (-38, 0)
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: start called in state 0
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: error (-38, 0)
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: Error (-38,0)
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: stop called in state 0
02-03 15:55:08.566 15845-15845/com.example.sk.voiceapplication E/MediaPlayer: error (-38, 0)

I'm not sure that this is your entire problem, but I see that you are calling media player.prepare() in the doInBackground method of your AsyncTask, and then calling media player.start() in onPostExecute(), 我不确定这是否是您的全部问题,但是我看到您正在AsyncTask的doInBackground方法中调用media player.prepare(),然后在onPostExecute()中调用media player.start(),

But you haven't waited for the player to finish initializing after the call to prepare(). 但是您没有等到调用prepare()后,播放器完成初始化。 You need to use an OnPreparedListener, and wait for that notification before interacting with the player. 您需要使用OnPreparedListener,并在与播放器进行交互之前等待该通知。

I was doing this mistake, the file name of streamed mp3 song in url in new Player().execute("http://www.url_of_song.mp3"); 我在做这个错误,是new Player().execute("http://www.url_of_song.mp3");中url中流mp3歌曲的文件名new Player().execute("http://www.url_of_song.mp3"); inside onClick method contains spaces and capital letters thus the song couldnt be streamed. 内部的onClick方法包含空格和大写字母,因此无法流式播放歌曲。 So be aware of such silly mistakes. 因此,请注意此类愚蠢的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - OpenGL - 仿真器与实际设备 - Android - OpenGL - Emulator vs Actual Device 代号一个MediaPlayer在Android设备上不起作用 - codename one mediaplayer not working on android device 在仿真器上工作但不在真正的Android设备上 - Working on Emulator but not on the real Android device Android RadioButton仅显示文本,而不显示设备模拟器中的实际按钮 - Android RadioButton shows only text, not the actual button in device emulator Android哈希代码不匹配:实际设备上出现onFacebookError,应用在模拟器上运行 - Android Hash code Mismatch : onFacebookError on actual device, app runs on emulator 后退按钮在模拟器上运行,但在Android设备中不运行 - back button is working on emulator but it is not working in device in android Android:Mediaplayer 可以在模拟器上运行,但不能在手机上运行 - Android: Mediaplayer works in emulator but not on phone Android App可在模拟器上运行,但在设备上启动时崩溃 - Android App working on Emulator but crashes at launch on device 将调试器安装到无法在Netbeans中运行的Android设备(不是模拟器) - Attatch debugger to Android Device not working in Netbeans (not emulator) Android套接字应用程序在模拟器上运行但不在设备上运行 - Android socket application running on emulator but not working on device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM