MediaPlayer错误(1,-1004)又名MEDIA_ERROR_IO尝试播放YouTube视频

时间:2013-03-12 22:09:11

标签: android youtube-api android-mediaplayer

我在尝试播放YouTube视频时遇到问题。删除所有不必要的代码后,项目如下所示。

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pell.foo"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pell.foo.FooActivity"
            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>

activity_foo.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_foo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="left|top"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:singleLine="true"
        android:text="@string/editText1_text"
        android:hint="@string/editText1_hint" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button1_text" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <VideoView
            android:id="@+id/videoView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>    

</LinearLayout>

的strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Foo</string>
    <string name="action_settings">Settings</string>
    <string name="editText1_text">psy gangnam style</string>
    <string name="editText1_hint">YouTube search request</string>
    <string name="button1_text">Search &amp; Play YouTube video</string>

</resources>

FooActivity.java

package com.pell.foo;

import android.os.Bundle;
import android.os.AsyncTask;
import android.app.Activity;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.VideoView;
import android.widget.MediaController;

import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;

import com.google.gdata.client.youtube.*;
import com.google.gdata.data.youtube.*;
import com.google.gdata.util.*;

public class FooActivity extends Activity {
    static final String TAG = "FooActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foo);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {          
            @Override
            public void onClick(View v) {
                class SearchAndPlayYouTubeVideoTask extends AsyncTask<Void, Void, String> {
                    protected String doInBackground(Void... v) {
                        try {
                            YouTubeService service = new YouTubeService("FooActivity");
                            YouTubeQuery query = new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/api/videos"));
                            query.setOrderBy(YouTubeQuery.OrderBy.RELEVANCE);
                            EditText editText = (EditText)findViewById(R.id.editText1);
                            String queryString = editText.getText().toString();
                            query.setFullTextQuery(queryString);
                            query.setSafeSearch(YouTubeQuery.SafeSearch.NONE);
                            VideoFeed videoFeed = service.query(query, VideoFeed.class);
                            for(VideoEntry videoEntry : videoFeed.getEntries() ) {
                                Log.i(TAG, videoEntry.getTitle().getPlainText());
                            }                           
                            VideoEntry videoEntry = videoFeed.getEntries().get(0);                          
                            YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
                            YouTubeMediaContent mediaContent = mediaGroup.getYouTubeContents().get(0);                          
                            String url = mediaContent.getUrl();                         
                            return url;
                        }
                        catch(ServiceException e) {
                            e.printStackTrace();
                            return null;
                        }
                        catch(MalformedURLException e) {
                            e.printStackTrace();
                            return null;
                        }
                        catch(IOException e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                    protected void onPostExecute(String s) {
                        try {
                            VideoView videoView = (VideoView)findViewById(R.id.videoView1);
                            videoView.setVideoURI(Uri.parse(s));
                            videoView.setMediaController(new MediaController(getApplicationContext()));
                            videoView.requestFocus();
                            videoView.start();
                        }
                        catch(IllegalArgumentException e) {
                            e.printStackTrace();
                        }
                        catch(IllegalStateException e) {
                            e.printStackTrace();
                        }
                    }
                }
                new SearchAndPlayYouTubeVideoTask().execute();
            }
        });
    }
}

在Android设备上运行此功能(4G手机和WiFi平板电脑)我收到MEDIA_ERROR_IO即MediaPlayer错误(1,-1004)。怎么了?怎么解决?

UPD。以下代码是针对该问题的快速而肮脏的修复:

VideoEntry videoEntry = videoFeed.getEntries().get(YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
String url = null;
for(YouTubeMediaContent mediaContent : mediaGroup.getYouTubeContents()) {
    String mediaLocation = mediaContent.getUrl();
    if(mediaLocation.matches("^rtsp.*")) {
        if(null == url ) {
            url = mediaLocation;
        }    
    }    
}

看来,VideoView只能播放rtsp视频流。

更好的解决方案是在WebView中嵌入视频播放。

0 个答案:

没有答案