- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
您好,我正在尝试实现一个背景由视频文件制作的 Activity 。下面发布的解决方案适用于大多数设备,nexus 6 Android 5.1 除外。视频正常播放,已缩放但未裁剪,因此未保留纵横比。
使用 Galaxy Note、HTC One m7、Xperia Z3、Nexus 5 进行测试。
代码如下:
public class MainActivity extends ActionBarActivity implements SurfaceHolder.Callback, OnClickListener
{
private static final int VIDEO_START_HACK_DELAY = 0;
private static final String BG_VIDEO_PATH = "/raw/construction";
private SurfaceView mSurface;
private Button mTour, mLogin;
private MediaPlayer mMediaPlayer;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_main);
setupUI();
}
private void setupUI()
{
mMediaPlayer = new MediaPlayer();
mSurface = (SurfaceView) findViewById(R.id.surface);
SurfaceHolder holder = mSurface.getHolder();
holder.addCallback(this);
mTour = (Button) findViewById(R.id.tour);
mLogin = (Button) findViewById(R.id.login);
mTour.setOnClickListener(this);
mLogin.setOnClickListener(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
startVideo(holder);
}
@SuppressLint("NewApi")
public void startVideo(SurfaceHolder holder)
{
String path = "android.resource://" + getPackageName() + BG_VIDEO_PATH;
Uri uri = Uri.parse(path);
try
{
android.view.ViewGroup.LayoutParams lp = mSurface.getLayoutParams();
mMediaPlayer.setDisplay(holder);
mMediaPlayer.setDataSource(this, uri);
mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
mMediaPlayer.setLooping(true);
mMediaPlayer.setOnPreparedListener(new OnPreparedListener()
{
@Override
public void onPrepared(MediaPlayer mp)
{
mHandler.postDelayed(new Runnable()
{
@Override
public void run()
{
mMediaPlayer.start();
}
}, VIDEO_START_HACK_DELAY);
}
});
mMediaPlayer.prepareAsync();
}
catch (Exception e)
{
Log.e("VID", "Problem starting video bg", e);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// TODO Auto-generated method stub
}
@Override
public void onBackPressed()
{
mSurface.setVisibility(View.INVISIBLE);
mMediaPlayer.stop();
super.onBackPressed();
}
@Override
protected void onDestroy()
{
mMediaPlayer.stop();
mMediaPlayer.release();
super.onDestroy();
}
}
这是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/tour"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:padding="10dp"
android:text="@string/ma_tour"/>
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="10dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:text="@string/ma_log_in"/>
</LinearLayout>
</RelativeLayout>
目标 sdk 设置为 22。您是否经历过这种行为并可以提供任何建议?
最佳答案
这是我在我的一个应用程序中的做法:
在 MediaPlayer 对象上设置 OnPreparedListener。
mediaPlayer.setOnPreparedListener(this);
当 public void onPrepared(MediaPlayer mp)
被调用时,调整 SurfaceView
的大小,使其产生裁剪视频的错觉。 Log.v(LOG_TAG, "onPrepared 调用 MediaPlayer"); //调整视频大小使其适合屏幕 int videoWidth = mInitialVideoPlayer.getVideoWidth(); int videoHeight = mInitialVideoPlayer.getVideoHeight(); float videoProportion = (float) videoWidth/(float) videoHeight;
ViewGroup.LayoutParams lp = mSplashVideoSurfaceView.getLayoutParams();
// We always want to fill entire view port. So, we keep the smallest dimension and proportionately
// scale the other dimension.
if (videoWidth < videoHeight) {
lp.width = UiUtils.sScreenWidth;
lp.height = (int) ((float) UiUtils.sScreenWidth / videoProportion);
} else {
lp.width = (int) (videoProportion * (float) UiUtils.sScreenHeight);If you are targetting API level 14 and above, <code>TextureView</code> allows you to play cropped video
lp.height = UiUtils.sScreenHeight;
}
Log.v(LOG_TAG, "old video size: " + videoWidth + ", " + videoHeight);
Log.v(LOG_TAG, "screen size: " + UiUtils.sScreenWidth + ", " + UiUtils.sScreenHeight);
Log.v(LOG_TAG, "new video size: " + lp.width + ", " + lp.height);
mSplashVideoSurfaceView.setLayoutParams(lp);
关于android - Nexus 6 和 MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182917/
我在 logcat 中收到一条警告消息 W/MediaPlayer-JNI(16795): MediaPlayer finalized without being released 请告诉我我错在哪里
我有以下代码。 例如: if(player1 != null){ if(player1.isPlaying()){ //check if it playing //other
我在安卓系统工作。我正在创建一个正在运行音频文件的 mediaPlayer。我有 10 个按钮。我为每个按钮分配了不同的 url。因此,当我按下按钮 1 时,将播放与按钮 1 相关的 url 歌曲。然
我正在使用 MediaPlayer。我想制作在线流媒体广播。现在我有这个 mediaPlayer 对象(MediaPlayer 的),在 fragment 1 中说,当我在 fragment 1 上时
我使用了以下代码: mp = MediaPlayer.create(this, Uri.parse("file://"+filePath)); mp.start(); 这很好用。然后我想播放文件夹中的
我通过下载新文件来启动我的应用程序,如果有任何文件开始播放循环。然后我每 x 秒触发一个任务来寻找新媒体。调用任务后,视频将停止播放,并显示错误 MediaPlayer finalized witho
我的应用程序允许用户从多个音频流中进行选择,当用户点击其中一个播放按钮时,将调用以下代码: @Override public void onClick(View view) { mMediaP
我正在尝试在我的 ViewController 中播放视频。 Mediaplayer.framework 和所有 Headers 都在我的 Frameworks 文件夹中,包括 MediaPlayer
我正在使用 MediaPlayer 和 SurfaceView 播放本地视频文件。 SurfaceView 是 Activity 中的唯一控件,而我的视频文件是 QVGA 或其他。问题是视频被拉伸(s
我正在使用 JavaFX 的 MediaPlayer 播放 mp3 文件,getCurrentTime() 工作正常,但是当我在某个时候使用 seek() 方法时, getCurrentTime()
我将音量设置为 0.0,然后在 while 循环中一点一点地改变音量。然而,音量从 0.0 跳到 1.0?如何顺利改变音量?我试过了 public class EngineSound extends
我为“播放”和“暂停”按钮创建了两个 onClick 方法,但如果先点击“暂停”,然后点击“播放”按钮,则音乐不会播放。下面是代码 package com.example.myaudio; impor
我用 MediaPlayer 控件创建了一个绑定(bind)服务,它可以加载和播放音频。但碰巧的是,有时 mediaPlayer.prepare(); 速度不够快,并不总是准备好播放。 那么我怎样才能
我正在我的应用程序中播放从网络流式传输的 mp3 文件,一些 mp3 文件有奇怪的行为:mediaPlayer.getCurrentPosition() 大于 mediaPlayer.getDurat
我想制作广播播放器示例,但我有一些错误。我无法修复它。 我的源代码 String url = ""; //Shoutcast Radio URL mp = new MediaPlayer();
我知道有一些类似的问题,但没有一个回答我的问题。当我点击一个按钮时,媒体播放器被调用,并且它出现在日志中。 06-02 00:20:38.980 26035-26035/myapp.com.facad
我正在尝试从 URL 流式传输音频。该代码适用于其他 URL,但其中一个 URL 在 OnPrepared 方法中失败,返回此错误代码:(1, -2147483648)。我看过一些人说这是因为权限,但
我在我的 Android 应用程序中使用 MediaPlayer 来播放视频。当通过 mediaPlayer.pause() 方法暂停播放并调用 mediaPlayer.seekTo(msec) 时,
我的应用程序在 MediaPlayer 流媒体方面存在问题,特别是在 Nexus 5 上。我不确定这是 Nexus 5 还是 API 级别 19 导致的问题。基本上我的 MediaPlayer 准备好
不过,我正在使用 RingtoneManager 来获取 Ringtone。 此信息已包含在 this question and answer 中,但 Google 员工不太可能找到它,除非他们已经知
我是一名优秀的程序员,十分优秀!