Android videoview不播放rtsp

Posted

技术标签:

【中文标题】Android videoview不播放rtsp【英文标题】:Android videoview doesn´t play rtsp 【发布时间】:2013-03-08 05:11:32 【问题描述】:

我有以下简单的 android 应用程序。

package com.example.videotest;

public class MainActivity extends Activity 

private Button playButton;
private Button clearButton;
private Button stopButton;
private Button defaultButton;
private VideoView mVideoView;
private EditText editText;
private String videoAddress;
private static final String TAG = "videoTest"; 
private String keyValue = "videoAddress";
private String defaultAddress = "rtsp://v6.cache7.c.youtube.com/CjYLENy73wIaLQlpqssvHCj9nxMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYLXw9ZeX2OLcUAw=/0/0/0/video.3gp";


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    SharedPreferences prefs = this.getSharedPreferences(TAG, 0);

    videoAddress = prefs.getString(keyValue, defaultAddress);

    playButton = (Button)findViewById(R.id.button1);
    clearButton = (Button)findViewById(R.id.button2);
    stopButton = (Button)findViewById(R.id.button3);
    defaultButton = (Button)findViewById(R.id.button4);

    mVideoView = (VideoView) findViewById(R.id.videoView1);
    editText = (EditText)findViewById(R.id.editText1);
    editText.setText(videoAddress);

    defaultButton.setOnClickListener(new OnClickListener() 

        public void onClick(View arg0) 

             editText.setText(defaultAddress);
             SharedPreferences appSettings =  getSharedPreferences(TAG, 0);
             SharedPreferences.Editor prefEditor = appSettings.edit();          
             prefEditor.putString(keyValue, defaultAddress);    
             prefEditor.commit();


        
    );

    stopButton.setOnClickListener(new OnClickListener() 

        public void onClick(View arg0) 

             mVideoView.stopPlayback();


        
    );

    playButton.setOnClickListener(new OnClickListener() 

        public void onClick(View arg0) 


            videoAddress = editText.getText().toString();
             SharedPreferences appSettings =  getSharedPreferences(TAG, 0);
             SharedPreferences.Editor prefEditor = appSettings.edit();          
             prefEditor.putString(keyValue, videoAddress);  
             prefEditor.commit();
             //PlayRtspStream("rtsp://10.0.0.32:5004/aac5.sdp");
             PlayRtspStream(videoAddress);


        
    );

    clearButton.setOnClickListener(new OnClickListener() 

        public void onClick(View arg0) 

             editText.setText("");
             SharedPreferences appSettings =  getSharedPreferences(TAG, 0);
             SharedPreferences.Editor prefEditor = appSettings.edit();          
             prefEditor.putString(keyValue, "");    
             prefEditor.commit();


        
    );




private void PlayRtspStream(String rtspUrl)  
    mVideoView.setVideoPath(rtspUrl);
    //mVideoView.setVideoURI(Uri.parse(rtspUrl));  
    mVideoView.requestFocus();  
    mVideoView.start();  
  

@Override
public boolean onCreateOptionsMenu(Menu menu) 
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;


AndroidManifest.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.videotest"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.videotest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

当我运行应用程序并将视频视图的视频路径设置为默认地址时,我收到提示“无法播放视频”。这是 logcat 文件。

03-07 21:09:51.305: D/MediaPlayer(20031): Couldn't open file on client side, trying server side
03-07 21:09:51.305: E/MediaPlayer(20031): Unable to to create media player
03-07 21:09:51.305: W/VideoView(20031): Unable to open content: rtsp://v6.cache7.c.youtube.com/CjYLENy73wIaLQlpqssvHCj9nxMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYLXw9ZeX2OLcUAw=/0/0/0/video.3gp
03-07 21:09:51.305: W/VideoView(20031): java.io.IOException: setDataSource failed.: status=0x80000000
03-07 21:09:51.305: W/VideoView(20031):     at android.media.MediaPlayer._setDataSource(Native Method)
03-07 21:09:51.305: W/VideoView(20031):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:958)
03-07 21:09:51.305: W/VideoView(20031):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:946)
03-07 21:09:51.305: W/VideoView(20031):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:899)
03-07 21:09:51.305: W/VideoView(20031):     at android.widget.VideoView.openVideo(VideoView.java:236)
03-07 21:09:51.305: W/VideoView(20031):     at android.widget.VideoView.setVideoURI(VideoView.java:198)
03-07 21:09:51.305: W/VideoView(20031):     at android.widget.VideoView.setVideoURI(VideoView.java:188)
03-07 21:09:51.305: W/VideoView(20031):     at android.widget.VideoView.setVideoPath(VideoView.java:184)
03-07 21:09:51.305: W/VideoView(20031):     at com.example.videotest.MainActivity.PlayRtspStream(MainActivity.java:109)
03-07 21:09:51.305: W/VideoView(20031):     at com.example.videotest.MainActivity.access$6(MainActivity.java:108)
03-07 21:09:51.305: W/VideoView(20031):     at com.example.videotest.MainActivity$3.onClick(MainActivity.java:85)
03-07 21:09:51.305: W/VideoView(20031):     at android.view.View.performClick(View.java:4204)
03-07 21:09:51.305: W/VideoView(20031):     at android.view.View$PerformClick.run(View.java:17355)
03-07 21:09:51.305: W/VideoView(20031):     at android.os.Handler.handleCallback(Handler.java:725)
03-07 21:09:51.305: W/VideoView(20031):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 21:09:51.305: W/VideoView(20031):     at android.os.Looper.loop(Looper.java:137)
03-07 21:09:51.305: W/VideoView(20031):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-07 21:09:51.305: W/VideoView(20031):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 21:09:51.305: W/VideoView(20031):     at java.lang.reflect.Method.invoke(Method.java:511)
03-07 21:09:51.305: W/VideoView(20031):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-07 21:09:51.305: W/VideoView(20031):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-07 21:09:51.305: W/VideoView(20031):     at dalvik.system.NativeStart.main(Native Method)
03-07 21:09:51.315: D/VideoView(20031): Error: 1,0

关于我做错了什么有什么想法吗?

【问题讨论】:

请为您的清单添加互联网权限.. 2.2没有网络权限怎么玩 非常感谢Praful,你的钱是对的。你可以发表你的评论作为答案,这样我就可以给你信用吗?再次感谢。 【参考方案1】:

由Praful解决,在Manifest.xml中添加Internet权限

<uses-permission android:name="android.permission.INTERNET"/>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.videotest"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.videotest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【讨论】:

以上是关于Android videoview不播放rtsp的主要内容,如果未能解决你的问题,请参考以下文章

Android videoview不播放rtsp

Android 使用VideoView实现简单视频播放

如何检测 VideoView 何时开始播放(Android)?

VideoView 不在 android Q 中播放视频,但在较低的 android 版本中工作正常

无法在android的videoview中播放来自url的视频如何在videoview中播放来自URL的视频?

播放android studio时片段中的VideoView没有声音